module.exports = (sequelize, DataTypes) => {
    const Counter = sequelize.define(
        'Counter',
        {
            id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                autoIncrement: true,
                primaryKey: true
            },
            name: {
                type: DataTypes.STRING,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '厂商名不能为空'
                    }
                },
                comment: '厂商名'
            },
            no: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '厂编不能为空'
                    }
                },
                comment: '厂编'
            },
            status: {
                type: DataTypes.TINYINT.UNSIGNED,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '状态不能为空'
                    }
                },
                comment: '状态:1.申请进场(会计审核完毕) 2.已进场(技术课分配磅秤) 9.已退场(技术课检查归还磅秤正常)'
            },
            begin: {
                type: DataTypes.DATEONLY,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '进场日期不能为空'
                    },
                    isDate: {
                        args: true,
                        msg: '进场日期必须是一个日期格式'
                    }
                },
                comment: '进场日期'
            },
            end: {
                type: DataTypes.DATEONLY,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '到期日期不能为空'
                    },
                    isDate: {
                        args: true,
                        msg: '到期日期必须是一个日期格式'
                    }
                },
                comment: '到期日期'
            },
            month: {
                type: DataTypes.VIRTUAL,
                get() {
                    let endDate = new Date(this.end)
                    let beginDate = new Date(this.begin)
                    if (this.end !== null) {
                        return (endDate.getFullYear() - beginDate.getFullYear()) * 12 + endDate.getMonth() - beginDate.getMonth()
                    } else {
                        return null
                    }
                },
                set(val) {
                    this.setDataValue('begin', new Date())
                    const endDate = new Date()
                    endDate.setMonth(endDate.getMonth() + val)
                    this.setDataValue('end', endDate)
                },
                validate: {
                    isInt: {
                        msg: '租用期限必须是一个整数'
                    }
                }
            },
            out: {
                type: DataTypes.DATEONLY,
                allowNull: true,
                validate: {
                    isDate: {
                        args: true,
                        msg: '退场日期必须是一个日期格式'
                    }
                },
                comment: '实际退场日期'
            },
            remark: {
                type: DataTypes.STRING,
                comment: '备注'
            },
            store_id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: false,
                validate: {
                    notNull: {
                        msg: '所属分店不能为空'
                    }
                },
                references: {
                    model: 'Store',
                    key: 'id'
                },
                comment: '所属分店的ID'
            },
            device_id: {
                type: DataTypes.BIGINT.UNSIGNED,
                allowNull: true,
                references: {
                    model: 'Device',
                    key: 'id'
                },
                comment: '租用的设备的ID'
            }
        },
        {
            tableName: 'counter'
        }
    )

    Counter.associate = function(models) {
        models.Counter.belongsTo(models.Device, { foreignKey: 'device_id', targetKey: 'id' })
        models.Counter.belongsTo(models.Store, { foreignKey: 'store_id', targetKey: 'id' })
    }

    return Counter
}
